package servlet;

import java.io.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.*;
import javax.servlet.http.*;

import common.MyNode;
import common.MyTreeNode;
import common.Property;
import common.SkillManInstance;
import common.SkillManSchema;
import common.SkillManSearch;
import common.SkillManTaxonomy;

public class internalSearch extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String cwd=Property.skillMgmHome;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		
		SkillManSchema schema=new SkillManSchema();
		schema.setCWD(Property.skillMgmHome);
		schema.load();
		
		SkillManTaxonomy tax=schema.getTaxonomy();
		Vector<common.MyNode> tree=tax.getTaxonomy();
		
		SkillManInstance base = new SkillManInstance(schema, " .owl");
		base.setCWD(Property.instances);
		base.loadDefault(Property.skillMgmHome+"default_instance.owl");
		base.setPropertyValue(SkillManInstance.foafNS+"Person", SkillManInstance.foafNS+"name" ,"Jocopoco Majoco");
		
		SkillManSearch search=new SkillManSearch(schema, base, Property.instances );
		search.loadFiles();
		search.loadOnts();
		search.compareToAll();
		Vector<MyTreeNode> mtVector =new Vector<MyTreeNode>();
		MyNode na;
		for (int i = 0; i < tree.size(); i++) {
			MyNode n = (MyNode)tree.get(i);
			MyTreeNode mt=new MyTreeNode();
			mt.setNome((String)(n.child.getLocalName())+" - ("+search.getNumBySkill((String)(n.child.getLocalName()))+")");
			mt.setNodeId(Integer.toString(i+1));
			mt.setParentNodeId(Integer.toString(0));
			for(int j=i;j>=0;j--){
				System.out.println("Entro in for");
				na=tree.get(j);
				if(na.depth==n.depth-1){
					mt.setParentNodeId(Integer.toString(j+1));
					break;
				}
			}
			
			mt.setUrl((String)(n.child.getLocalName()));
			System.out.println("MyTreeNode: "+mt.getNome()+" "+search.getNumBySkill(mt.getNome())+" "+" "+mt.getNodeId()+" "+mt.getParentNodeId()+" "+mt.getUrl());
			mtVector.add(mt);
		}
		
		session.setAttribute("mtVector", mtVector);
		session.setAttribute("search", search);	
		RequestDispatcher dispatcher = request.getRequestDispatcher("internalSearch.jsp");
		dispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
